<?php
namespace app\admin\controller;

use think\Loader;
use think\Db;
use app\common\tools\jsonRPCClient;

/**
* 用户管理
* @author aierui github  https://github.com/Aierui
* @version 1.0 
*/
class Purse extends Admin
{

	function _initialize()
    {
        parent::_initialize();
    }

    //钱包地址
    public function address(){

    	$admin = session('admin')['userinfo'];

    	$user = Db::name('user')->where('id',$admin['id'])->find();
    	// print_r($admin);exit;
    	$this->assign('info',$user);
    	return view();
    }

    //外网钱包地址
    public function external(){

        $controller = request()->controller();
        $action = request()->action();
            //验证支付密码
        if(session('second') !== 'second'){
            $this->redirect('user/second',['controller'=>$controller,'action'=>$action]);
        }
            
        $admin = session('admin')['userinfo'];
        $info = Db::name('ex_address')->where('uid',$admin['id'])->find();
        // print_r($admin);exit;
        if($this->request->isPost()){
            $data = $this->request->post();
            unset($data['username']);
            if($info){
                //存在更新
                $res = Db::name('ex_address')->where('uid',$admin['id'])->update($data);
            }else{
                //不存在新增
                $data['uid'] = $admin['id'];
                $res = Db::name('ex_address')->insert($data);
            }
            if($res){
                        $this->error(info(lang('Submit success'),0)['msg']);
                    }else{
                        $this->error(info(lang('Submit failure'),0)['msg']);
                    }

        }
        $this->assign('info',$info);
        $this->assign('user',$admin);
        return view();
    }

    public function convert()
    {
        $controller = request()->controller();
        $action = request()->action();
            //验证支付密码
        if(session('second') !== 'second')
        {
            $this->redirect('user/second',['controller'=>$controller,'action'=>$action]);
        }
            
        $admin = session('admin')['userinfo'];
        $info = Db::name('user')->where('id',$admin['id'])->find();

        // print_r($admin);exit;
        if($this->request->isPost()){
            $data = $this->request->post();
            
            if(is_numeric($data['convert_num']))
            {

                if($data['convert_num'] < $info['transaction'])
                {
                    $convert['transaction']=$info['transaction'];
                    $convert['source']=$info['source'];

                    Db::name('user')->where('id',$admin['id'])->setDec('transaction',$data['convert_num']);
                    $res = Db::name('user')->where('id',$admin['id'])->setInc('source',$data['convert_num']*5);
                    $info = Db::name('user')->where('id',$admin['id'])->find();
                    $convert['uid']=$admin['id'];
                    $convert['transaction_after']=$info['transaction'];
                    $convert['source_after']=$info['source'];
                    $convert['convert_num']=$data['convert_num'];
                    $convert['convert_num_after']=$data['convert_num']*5;
                    Db::name('convert_source')->insert($convert);
                    if($res){
                        $this->error(info(lang('Submit success'),0)['msg']);
                    }else{
                        $this->error(info(lang('Submit failure'),0)['msg']);
                    }

                }else
                {
                    
                    $this->error(info(lang('The input value should not exceed the transaction value'),0)['msg']);

                }

                
            }



        }
        $this->assign('info',$info);
        $this->assign('user',$admin);
        return view();
    }

    public function getconvert()
    {

        if(!request()->isAjax()) {
            $this->error(lang('Request type error'), 4001);
        }
        $request = request()->param();
        unset($request['/admin/purse/getconvert']);
        
        $admin = session('admin')['userinfo'];
        $info = Db::name('user')->where('id',$admin['id'])->find();

        $request = $this->fmtRequest($request);
        $data = Db::name('convert_source')->order('id desc')->where('uid',$admin['id'])->where( $request['map'] )->select();

        
        //$data = $this->_fmtData( $data );
        //$data = $this->fmtBuylistData( $data );

        //$data['total'] = 100;
        return $data;
    }


    public function weighted2transform()
    {
        $controller = request()->controller();
        $action = request()->action();
            //验证支付密码
        if(session('second') !== 'second')
        {
            $this->redirect('user/second',['controller'=>$controller,'action'=>$action]);
        }
            
        $admin = session('admin')['userinfo'];
        $info = Db::name('user')->where('id',$admin['id'])->find();

        // print_r($admin);exit;
        if($this->request->isPost()){
            $data = $this->request->post();
            
            if(is_numeric($data['convert_num']))
            {

                if($data['convert_num'] < $info['transaction'])
                {
                    $convert['transaction']=$info['transaction'];
                    $convert['weighted']=$info['weighted'];

                    Db::name('user')->where('id',$admin['id'])->setDec('weighted',$data['convert_num']);
                    $res = Db::name('user')->where('id',$admin['id'])->setInc('transaction',$data['convert_num']*5);
                    $info = Db::name('user')->where('id',$admin['id'])->find();
                    $convert['uid']=$admin['id'];
                    $convert['transaction_after']=$info['transaction'];
                    $convert['weighted_after']=$info['weighted'];
                    $convert['convert_num']=$data['convert_num'];
                    $convert['convert_num_after']=$data['convert_num'];
                    Db::name('convert_weighted')->insert($convert);
                    if($res){
                        $this->error(info(lang('Submit success'),0)['msg']);
                    }else{
                        $this->error(info(lang('Submit failure'),0)['msg']);
                    }

                }else
                {
                    
                    $this->error(info(lang('The input value should not exceed the transaction value'),0)['msg']);

                }

                
            }



        }
        $this->assign('info',$info);
        $this->assign('user',$admin);
        return view();
    }

    public function getconvertweighted()
    {

        if(!request()->isAjax()) {
            $this->error(lang('Request type error'), 4001);
        }
        $request = request()->param();
        unset($request['/admin/purse/getconvertweighted']);
        
        $admin = session('admin')['userinfo'];
        $info = Db::name('user')->where('id',$admin['id'])->find();

        $request = $this->fmtRequest($request);
        $data = Db::name('convert_weighted')->order('id desc')->where('uid',$admin['id'])->where( $request['map'] )->select();

        
        //$data = $this->_fmtData( $data );
        //$data = $this->fmtBuylistData( $data );

        //$data['total'] = 100;
        return $data;
    }

    public function weighted2tmbc()
    {

        $controller = request()->controller();
        $action = request()->action();
            //验证支付密码
        if(session('second') !== 'second')
        {
            $this->redirect('user/second',['controller'=>$controller,'action'=>$action]);
        }
            
        $admin = session('admin')['userinfo'];
        $info = Db::name('user')->where('id',$admin['id'])->find();
        $max_dt = Db::name('trade')->max('dt');
        $qu = Db::name('trade')->where('dt',$max_dt)->find();

        // print_r($admin);exit;
        if($this->request->isPost()){
            $data = $this->request->post();

            //判断是否打开开关
            $config = Db::name('config')->find();
            if($config['tmbc_convert']==0)
            {
                $this->error(info(lang('The current TMBC quota has been completed. Please wait for the new TMBC conversion time.'),0)['msg']);
            }


            
            if(is_numeric($data['convert_num']))
            {
                if($data['convert_num']>$info['weighted'])
                {
                    $this->error(info(lang('Quantity submitted exceeds maximum quantity.'),0)['msg']);

                }

                if($data['convert_num'] <= $info['weighted'])
                {
                    $convert['tmbc']=$info['tmbc'];
                    $convert['weighted']=$info['weighted'];


                    Db::name('user')->where('id',$admin['id'])->setDec('weighted',$data['convert_num']);
                    $res = Db::name('user')->where('id',$admin['id'])->setInc('tmbc',$data['convert_num']/$qu['rmb_price']);
                    $info = Db::name('user')->where('id',$admin['id'])->find();
                    $convert['uid']=$admin['id'];
                    $convert['tmbc_after']=$info['tmbc'];
                    $convert['weighted_after']=$info['weighted'];
                    $convert['convert_num']=$data['convert_num'];
                    $convert['convert_num_after']=$data['convert_num']/$qu['rmb_price'];
                    Db::name('convert_tmbc')->insert($convert);
                    if($res){
                        $this->error(info(lang('Submit success'),0)['msg']);
                    }else{
                        $this->error(info(lang('Submit failure'),0)['msg']);
                    }

                }else
                {
                    
                    $this->error(info(lang('Quantity submitted exceeds maximum quantity.'),0)['msg']);

                }

                
            }



        }
        $this->assign('info',$info);
        $this->assign('user',$admin);
        $this->assign('qu', $qu);
        return view();

    }

    public function getconverttmbc()
    {

        if(!request()->isAjax()) {
            $this->error(lang('Request type error'), 4001);
        }
        $request = request()->param();
        unset($request['/admin/purse/getconverttmbc']);
        
        $admin = session('admin')['userinfo'];
        $info = Db::name('user')->where('id',$admin['id'])->find();

        $request = $this->fmtRequest($request);
        $data = Db::name('convert_tmbc')->order('id desc')->where('uid',$admin['id'])->where( $request['map'] )->select();

        
        //$data = $this->_fmtData( $data );
        //$data = $this->fmtBuylistData( $data );

        //$data['total'] = 100;
        return $data;
    }




    public function receivables()
    {
        //获取左区的验证码
        $admin = session('admin')['userinfo'];
        //获取右区的验证码
        $ret = Db::name('user')->where('id',$admin['id'])->find();

        
        $this->assign('ret',$ret); 
        return view();
    }

    /**
     * 交易值转出
     * @return \think\response\View
     */
    public function transform()
    {
        $controller = request()->controller();
        $action = request()->action();
            //验证支付密码
        if(session('second') !== 'second')
        {
            $this->redirect('user/second',['controller'=>$controller,'action'=>$action]);
        }

        $eth = "";
        if($this->request->isGet())
        {
            $data = $this->request->get();
            if(isset($data['eth']))
            {
                $eth = $data['eth'];
            }
            
        }
            
        $admin = session('admin')['userinfo'];
        $info = Db::name('user')->where('id',$admin['id'])->find();

        // print_r($admin);exit;
        if($this->request->isPost())
        {
            $data = $this->request->post();

            if(!is_numeric($data['transform_num']))
            {
                $this->error(info(lang('Please input the correct number'),0)['msg']);
            }
            if(is_numeric($data['transform_num']))
            {
                $eth_user = Db::name('user')->where('eth',$data['eth'])->find();
                
                if(!$eth_user)
                {

                    
                    $this->error(info(lang('Please input the correct eth address'),0)['msg']);
                }

                if($data['transform_num']>10000)
                {

                    
                    $this->error(info(lang('Maximum transfer per time is 10000'),0)['msg']);
                }

                if($data['transform_num'] < $info['transaction'])
                {
                    $transaction['transaction']=$info['transaction'];

                    $transaction['rec_transaction']=$eth_user['transaction'];

                    $tr_dec = Db::name('user')->where('id',$admin['id'])->setDec('transaction',$data['transform_num']);
                    $tr_inc = Db::name('user')->where('eth',$data['eth'])->setInc('transaction',$data['transform_num']);


                    $info = Db::name('user')->where('id',$admin['id'])->find();
                    $eth_user = Db::name('user')->where('eth',$data['eth'])->find();

                    $transaction['uid']=$admin['id'];
                    $transaction['transaction_after']=$info['transaction'];
                    $transaction['transform_num']=$data['transform_num'];
                    $transaction['eth']=$data['eth'];
                    $transaction['rec_transaction_after']=$eth_user['transaction'];
                    $transaction['to_username']=$eth_user['username'];
                    $transaction['to_uid']=$eth_user['id'];
                    $transaction['from_username']=$info['username'];
                    $tr_ins = Db::name('transaction')->insert($transaction);
                    if($tr_dec && $tr_inc && $tr_ins){
                        $this->error(info(lang('Submit success'),0)['msg']);
                    }else{
                        $this->error(info(lang('Submit failure'),0)['msg']);
                    }

                }else
                {
                    
                    $this->error(info(lang('The input value should not exceed the transaction value'),0)['msg']);

                }

                
            }

        }
        $this->assign('eth',$eth);
        $this->assign('info',$info);
        $this->assign('user',$admin);
        return view();
    }

    /**
     * TMBC转出
     * @return \think\response\View
     */
    public function TMBC2transfer()
    {

        $controller = request()->controller();
        $action = request()->action();
            //验证支付密码
        if(session('second') !== 'second')
        {
            $this->redirect('user/second',['controller'=>$controller,'action'=>$action]);
        }

        $eth = "";
        if($this->request->isGet())
        {
            $data = $this->request->get();
            if(isset($data['eth']))
            {
                $eth = $data['eth'];
            }
            
        }
            
        $admin = session('admin')['userinfo'];
        $info = Db::name('user')->where('id',$admin['id'])->find();

        // print_r($admin);exit;
        if($this->request->isPost())
        {
            $data = $this->request->post();

            if(!is_numeric($data['transform_num']))
            {
                $this->error(info(lang('Please input the correct number'),0)['msg']);
            }
            if(is_numeric($data['transform_num']))
            {
                $eth_user = Db::name('user')->where('eth',$data['eth'])->find();
                
                if(!$eth_user)
                {

                    
                    $this->error(info(lang('Please input the correct eth address'),0)['msg']);
                }

                if($data['transform_num']<=0)
                {

                    
                    $this->error(info(lang('Please input the correct number'),0)['msg']);
                }

                if($data['transform_num']>$info['tmbc'])
                {

                    
                    $this->error(info(lang('Please input the correct number'),0)['msg']);
                }

                if($data['transform_num'] < $info['transaction'])
                {
                    

                    Db::name('user')->where('id',$admin['id'])->setDec('tmbc',$data['transform_num']);
                    Db::name('user')->where('eth',$data['eth'])->setInc('tmbc',$data['transform_num']);
                   
                    $info = Db::name('user')->where('id',$admin['id'])->find();
                    $eth_user = Db::name('user')->where('eth',$data['eth'])->find();

                    $transaction['uid']=$admin['id'];
                    $transaction['transform_num']=$data['transform_num'];
                    $transaction['eth']=$data['eth'];
                    $transaction['to_username']=$eth_user['username'];
                    $transaction['from_username']=$info['username'];
                    $res =Db::name('tmbc')->insert($transaction);
                    if($res){
                        $this->error(info(lang('Submit success'),0)['msg']);
                    }else{
                        $this->error(info(lang('Submit failure'),0)['msg']);
                    }

                }else
                {
                    
                    $this->error(info(lang('The input value should not exceed the transaction value'),0)['msg']);

                }

                
            }



        }
        $this->assign('eth',$eth);
        $this->assign('info',$info);
        $this->assign('user',$admin);
        return view();
    }



    public function gettransform()
    {

        if(!request()->isAjax()) {
            $this->error(lang('Request type error'), 4001);
        }
        $request = request()->param();
        unset($request['/admin/purse/gettransform']);
        
        $admin = session('admin')['userinfo'];
        $info = Db::name('user')->where('id',$admin['id'])->find();

        $request = $this->fmtRequest($request);
        $data = Db::name('transaction')->order('id desc')->where('uid',$admin['id'])->where( $request['map'] )->select();

        
        //$data = $this->_fmtData( $data );
        //$data = $this->fmtBuylistData( $data );

        //$data['total'] = 100;
        return $data;
    }

    public function gettmbc()
    {

        if(!request()->isAjax()) {
            $this->error(lang('Request type error'), 4001);
        }
        $request = request()->param();
        unset($request['/admin/purse/gettmbc']);

        $admin = session('admin')['userinfo'];
        $info = Db::name('user')->where('id',$admin['id'])->find();

        $request = $this->fmtRequest($request);
        $data = Db::name('tmbc')->order('id desc')->where('uid',$admin['id'])->where( $request['map'] )->select();

        
        //$data = $this->_fmtData( $data );
        //$data = $this->fmtBuylistData( $data );

        //$data['total'] = 100;
        return $data;
    }

    public function weight2tmbc()
    {
        $controller = request()->controller();
        $action = request()->action();
            //验证支付密码
        if(session('second') !== 'second')
        {
            $this->redirect('user/second',['controller'=>$controller,'action'=>$action]);
        }

        $eth = "";
        if($this->request->isGet())
        {
            $data = $this->request->get();
            if(isset($data['eth']))
            {
                $eth = $data['eth'];
            }
        }
            
        $admin = session('admin')['userinfo'];
        $info = Db::name('user')->where('id',$admin['id'])->find();

        // print_r($admin);exit;
        if($this->request->isPost())
        {
            $data = $this->request->post();

            
            if(is_numeric($data['transform_num']))
            {
                $trade_id = Db::name('trade')->max('id');
                $trade =  Db::name('trade')->where('id',$trade_id)->find();



                if($data['transform_num'] < $info['transaction'])
                {
                    $transaction['transaction']=$info['transaction'];
                    $transaction['tmbc']=$info['tmbc'];
                    $transaction['tran_num']=$data['transform_num'];
                    $transaction['tmbc_num']=$data['transform_num']/$trade['rmb_price'];
                    $transaction['eth'] = $info['eth'];
                    $transaction['flag'] = 0;


                    Db::name('user')->where('id',$admin['id'])->setDec('transaction',$data['transform_num']);
                    Db::name('user')->where('id',$admin['id'])->setInc('tmbc',$transaction['tmbc_num']);
                    

                    $info = Db::name('user')->where('id',$admin['id'])->find();

                    $transaction['transaction_after']=$info['transaction'];

                    $transaction['tmbc_after']=$info['tmbc'];


                    $res=Db::name('transaction_tmbc')->insert($transaction);
                    if($res)
                    {
                        $this->error(info(lang('Submit success'),0)['msg']);
                    }
                    else
                    {
                        $this->error(info(lang('Submit failure'),0)['msg']);
                    }

                }
                else
                {
                    
                    $this->error(info(lang('The input value should not exceed TMBC value'),0)['msg']);

                }

                
            }

        }
        $this->assign('eth',$eth);
        $this->assign('info',$info);
        $this->assign('user',$admin);
        return view();
    }


    public function transform2tmbc()
    {
        $controller = request()->controller();
        $action = request()->action();
            //验证支付密码
        if(session('second') !== 'second')
        {
            $this->redirect('user/second',['controller'=>$controller,'action'=>$action]);
        }

        $eth = "";
        if($this->request->isGet())
        {
            $data = $this->request->get();
            if(isset($data['eth']))
            {
                $eth = $data['eth'];
            }
        }
            
        $admin = session('admin')['userinfo'];
        $info = Db::name('user')->where('id',$admin['id'])->find();

        // print_r($admin);exit;
        if($this->request->isPost())
        {
            $data = $this->request->post();

            
            if(is_numeric($data['transform_num']))
            {
                $trade_id = Db::name('trade')->max('id');
                $trade =  Db::name('trade')->where('id',$trade_id)->find();



                if($data['transform_num'] < $info['transaction'])
                {
                    $transaction['transaction']=$info['transaction'];
                    $transaction['tmbc']=$info['tmbc'];
                    $transaction['tran_num']=$data['transform_num'];
                    $transaction['tmbc_num']=$data['transform_num']/$trade['rmb_price'];
                    $transaction['eth'] = $info['eth'];
                    $transaction['flag'] = 0;


                    Db::name('user')->where('id',$admin['id'])->setDec('transaction',$data['transform_num']);
                    Db::name('user')->where('id',$admin['id'])->setInc('tmbc',$transaction['tmbc_num']);
                    

                    $info = Db::name('user')->where('id',$admin['id'])->find();

                    $transaction['transaction_after']=$info['transaction'];

                    $transaction['tmbc_after']=$info['tmbc'];


                    $res=Db::name('transaction_tmbc')->insert($transaction);
                    if($res)
                    {
                        $this->error(info(lang('Submit success'),0)['msg']);
                    }
                    else
                    {
                        $this->error(info(lang('Submit failure'),0)['msg']);
                    }

                }
                else
                {
                    
                    $this->error(info(lang('The input value should not exceed TMBC value'),0)['msg']);

                }

                
            }

        }
        $this->assign('eth',$eth);
        $this->assign('info',$info);
        $this->assign('user',$admin);
        return view();
    }

    public function tmbc2transform()
    {
        $controller = request()->controller();
        $action = request()->action();
            //验证支付密码
        if(session('second') != 'second')
        {
            $this->redirect('user/second',['controller'=>$controller,'action'=>$action]);
        }

        $eth = "";
        if($this->request->isGet())
        {
            $data = $this->request->get();
            if(isset($data['eth']))
            {
                $eth = $data['eth'];
            }
        }
            
        $admin = session('admin')['userinfo'];
        $info = Db::name('user')->where('id',$admin['id'])->find();

        // print_r($admin);exit;
        if($this->request->isPost())
        {
            $data = $this->request->post();

            
            if(is_numeric($data['transform_num']))
            {
                $trade_id = Db::name('trade')->max('id');
                $trade =  Db::name('trade')->where('id',$trade_id)->find();



                if($data['transform_num'] < $info['transaction'])
                {
                    $transaction['transaction']=$info['transaction'];
                    $transaction['tmbc']=$info['tmbc'];
                    $transaction['tran_num']=$data['transform_num']*$trade['rmb_price'];
                    $transaction['tmbc_num']=$data['transform_num'];
                    $transaction['eth'] = $info['eth'];
                    $transaction['flag'] = 1;


                    Db::name('user')->where('id',$admin['id'])->setInc('transaction',$data['transform_num']);
                    Db::name('user')->where('id',$admin['id'])->setDec('tmbc',$transaction['tmbc_num']);
                    

                    $info = Db::name('user')->where('id',$admin['id'])->find();

                    $transaction['transaction_after']=$info['transaction'];

                    $transaction['tmbc_after']=$info['tmbc'];


                    $res=Db::name('transaction_tmbc')->insert($transaction);
                    if($res)
                    {
                        $this->error(info(lang('Submit success'),0)['msg']);
                    }
                    else
                    {
                        $this->error(info(lang('Submit failure'),0)['msg']);
                    }

                }
                else
                {
                    
                    $this->error(info(lang('The input value should not exceed TMBC value'),0)['msg']);

                }

                
            }

        }
        $this->assign('eth',$eth);
        $this->assign('info',$info);
        $this->assign('user',$admin);
        return view();
    }

    protected function fmtRequest( $request = [] )
    {
        if( empty($request) ) {
            return $request;
        }
        $offset = 0;
        if (isset($request['offset']) && is_numeric($request['offset']) ) {
            $offset = $request['offset'];
            unset($request['offset']);
        }
        $limit = 5;
        if (isset($request['limit']) && is_numeric($request['limit']) ) {
            $limit = $request['limit'];
            unset($request['limit']);
        }
        $ret = [
            'offset'=>$offset,
            'limit'=>$limit,
            'map'=>$request
        ];
        return $ret;
    }



    /**
     * 交易值增值
     * @return \think\response\View
     */
    public function increment()
    {

        $admin = session('admin')['userinfo'];
        $info = Db::name('user')->where('id',$admin['id'])->find();

        if($this->request->isPost())
        {
            $data = $this->request->post();


            if(!is_numeric($data['num']))
            {
                $this->error(info(lang('Please input the correct number'),0)['msg']);
            }

            if(is_numeric($data['num']))
            {
                $user = Db::name('user')->where('id',$admin['id'])->find();

                if(!$user)
                {
                    $this->error(info(lang('User Information Error'),0)['msg']);
                }

                if($data['num']<=0)
                {
                    $this->error(info(lang('Please input the correct number'),0)['msg']);
                }

                if($data['num']>$info['transaction'])
                {

                    $this->error(info(lang('Sorry, your balance is insufficient.'),0)['msg']);
                }

                if($data['num'] < $info['transaction'])
                {

                    $map = [
                        'uid'   => $admin['id'],
                        'num'   => $data['num']
                    ];

                    $source = $data['num']*0.8*5;
                    $weighted = $data['num']*0.2;

                    $tr_dec = Db::name('user')->where('id',$admin['id'])->setDec('transaction',$data['num']);

                    $so_inc = Db::name('user')->where('id',$admin['id'])->setInc('source',$source);
                    $we_inc = Db::name('user')->where('id',$admin['id'])->setInc('weighted',$weighted);

                    $va_ins = Db::name('value_added')->insert($map);

                    if($tr_dec && $so_inc && $we_inc && $va_ins){

                        $this->success(info(lang('Submit success'),0)['msg']);
                    }else{
                        $this->error(info(lang('Submit failure'),0)['msg']);

                    }

                }
            }

        }
        $this->assign('info',$info);
        $this->assign('user',$admin);
        return view();
    }

    /**
     * 交易值增值信息
     * @return false|mixed|\PDOStatement|string|\think\Collection
     */
    public function increment_gettmbc()
    {

        if(!request()->isAjax()) {
            $this->error(lang('Request type error'), 4001);
        }
        $request = request()->param();
        unset($request['/admin/purse/increment_gettmbc']);

        $admin = session('admin')['userinfo'];

        $request = $this->fmtRequest($request);
        $data = Db::name('value_added')->order('id desc')->where('uid',$admin['id'])->where( $request['map'] )->select();

        foreach($data as &$value){
            $value['source']    = $value['num']*0.8*5;
            $value['weighted']  = $value['num']*0.2;
        }

        return $data;
    }

    /**
     * 提货增加交易值
     * @return \think\response\View
     */
    public function delivery()
    {

        $admin = session('admin')['userinfo'];

        if($this->request->isPost()){
            $file = $this->request->file('file_input');

            //对文件进行验证，判断是否是图片文件
            if(!$file)
            {
                $this->error(info(lang('Submit failure'),0)['msg']);
            }

            $path = ROOT_PATH . 'public/uploads/images/';

            $info = $file->move($path);
            if (!$info)
            {
                return ajax_return_error($file->getError());
            }

            $img = $this->request->root() . 'uploads/images/' . $info->getSaveName();

            $attach = [
                'uid'       => $admin['id'],
                'name'      => $info->getFilename(),
                'path'      => $img,
                'module'    => 'tmbc/purse',
                'mime'      => $info->getMime(),
                'md5'       => $info->md5(),
                'sha1'       => $info->sha1(),
                'size'       => $info->getSize(),
                'ext'       => $info->getExtension(),
                'create_time'       => time(),
            ];

            $img_id = Db::name('admin_attachment')->insertGetId($attach);

            $map['uid'] = $admin['id'];
            $map['img_src'] = $img_id;

            $res = Db::name('value_delivery')->insert($map);

            if($res){
                $this->success(info(lang('Submit success'),0)['msg']);
            }else{
                $this->error(info(lang('Submit failure'),0)['msg']);
            }

        }
        return view();
    }

    /**
     * 提货增加交易值
     * @return false|mixed|\PDOStatement|string|\think\Collection
     */
    public function delivery_list()
    {

        if(!request()->isAjax()) {
            $this->error(lang('Request type error'), 4001);
        }
        $request = request()->param();
        unset($request['/admin/purse/delivery_list']);

        $admin = session('admin')['userinfo'];

        $request = $this->fmtRequest($request);
        $data = Db::name('value_delivery')->order('status asc,id desc')->where('uid',$admin['id'])->where( $request['map'] )->select();

        foreach($data as &$value){
            $img = getImgUrl($value["img_src"]);
            $value['img_src']    = "<img style='width: 50px;height: 25px;' src='$img' />";
            if($value['status'] == 0){
                $value['status']  = '待审核';
            }elseif($value['status'] == 1){
                $value['status']  = '已审核';
            }else{
                $value['status'] = '无效';
            }
        }

//        print_r($data);exit;

        return $data;
    }

}